package cn.baiweigang.uu.db;

import cn.baiweigang.uu.model.AccountInfo;
import cn.baiweigang.uu.model.DataMap;
import cn.baiweigang.uu.model.StrategyInfo;
import cn.baiweigang.uu.utils.DButil;
import cn.baiweigang.uu.utils.DateUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.Date;
import java.util.List;

public class OrderHistoryDb extends BaseDb {
    public static void insert(StrategyInfo strategyInfo) {
        AccountInfo accountInfo = strategyInfo.getAccountInfo();
        long volume = strategyInfo.getCloseOrder().getVolume();
        double openOrderPrice = strategyInfo.getOpenOrder().getTradeAvgPrice().doubleValue();
        double closeOrderPrice = strategyInfo.getCloseOrder().getTradeAvgPrice().doubleValue();

        double profit = strategyInfo.getCloseOrder().getRealProfit().doubleValue();
        double tmp = strategyInfo.getCloseOrder().getTradeAvgPrice().subtract(
                strategyInfo.getOpenOrder().getTradeAvgPrice()).abs().doubleValue();
        double profitVolume = profit > 0 ? tmp : 0 - tmp;
        String orderSource = strategyInfo.getCloseOrder().getOrderSource();
        double fee = strategyInfo.getOpenOrder().getFee().
                add(strategyInfo.getCloseOrder().getFee()).doubleValue();

        double maxTpVolume = strategyInfo.getOpenOrder().getMaxTpVolume();
        if (profitVolume > maxTpVolume) maxTpVolume = profitVolume;
        double maxSlVolume = strategyInfo.getOpenOrder().getMaxSlVolume();
        if (profitVolume < maxSlVolume) maxSlVolume = profitVolume;

        long holdMinutes = (strategyInfo.getCloseOrder().getCreatedGmt() - strategyInfo.getOpenOrder().getCreatedGmt()) / 1000 / 60;
        String sql = String.format("insert into order_history " +
                        "(account_name,contract_code," +
                        "volume,open_gmt,close_gmt,hold_minutes," +
                        "open_order_price,close_order_price,profit_volume,profit,fee,max_sl_volume,max_tp_volume," +
                        "order_source,close_memo,strategy_name,strategy_status_list," +
                        "open_order,close_order) values " +
                        "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                accountInfo.getAccountName(), accountInfo.getContractCode().getCode(),
                volume, DateUtil.formatDateYMDHMS(new Date(strategyInfo.getOpenOrder().getCreatedGmt())), DateUtil.formatDateYMDHMS(new Date(strategyInfo.getCloseOrder().getCreatedGmt())), holdMinutes,
                openOrderPrice, closeOrderPrice, profitVolume, profit, fee, maxSlVolume, maxTpVolume,
                orderSource, strategyInfo.getForceCloseMemo(), strategyInfo.getName(), JSONObject.toJSONString(strategyInfo.getStrategyHistory()),
                JSONObject.toJSONString(strategyInfo.getOpenOrder()), JSONObject.toJSONString(strategyInfo.getCloseOrder()));
        DButil.update(sql);
    }

    public static List<DataMap> queryAllByCloseGmt(AccountInfo accountInfo, String fromDateTime, String toDateTime) {
        String sql = "select * from order_history where close_gmt >='" +
                fromDateTime + "' and close_gmt <'" + toDateTime +
                "' and account_name='" + accountInfo.getAccountName() +
                "' and contract_code='" + accountInfo.getContractCode().getCode() + "' ";
        return DButil.queryMultil(sql);
    }

    public static List<DataMap> queryLast(AccountInfo accountInfo, int size) {
        String sql = "select * from order_history where account_name='" + accountInfo.getAccountName() +
                "' and contract_code='" + accountInfo.getContractCode().getCode() +
                "' order by close_gmt desc limit " + size;
        return DButil.queryMultil(sql);
    }
}
